from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# 启动Chrome浏览器并打开百度
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")

# 找到百度的搜索框并输入“selenium”，然后提交表单
search_box = driver.find_element(By.CLASS_NAME,'s_ipt')
search_box.send_keys('selenium')
search_box.send_keys(Keys.RETURN)

# 等待搜索结果页面加载完全
time.sleep(5)

# 验证至少10个搜索结果
search_results = driver.find_elements(By.CSS_SELECTOR,'h3 > a')
assert len(search_results) >= 10, "At least 10 search results not found."

# 验证搜索框文本包含“selenium”
search_box_text = driver.find_element(By.CLASS_NAME,'s_ipt').get_attribute('value')
assert 'selenium' in search_box_text, "Search box does not contain 'selenium' text."

# 验证第一个搜索结果链接是Selenium官网
first_result = search_results[0].get_attribute('href')
assert first_result == 'https://www.selenium.dev/', "First search result is not from Selenium official website."



# 关闭浏览器
driver.quit()
